//Battle.js
//Battle System
function delay(sec)
{
	var start = GetTime();
	while(start + (1000*sec) > GetTime());
}
var combatants = new Array();

function Combatant(name,image)
{
	if(image == undefined)
	{
		image = "Unknown.png";
	}
	this.name = name;
	this.x = 50;
	this.y = 50;
	this.image = image;
	
	this.min_hp = 0;
	this.hp = 50;
	this.max_hp = 50;
	this.str = 10;
	
	this.type = "monster";
}

Combatant.prototype.clone = function()
{
	var obj = new Combatant();
	for (var i in this)
		obj[i] = this[i];
	return obj;
}

function startBattle()
{
	var player = new Combatant("player");
	player.type = "player";
	player.x = 220;
	player.str = 15;
	player.image = "placeholder_player.png";
	
	combatants.push(player.clone());
}

function GetTotalMonsters()
{
	var total_monsters = 0;
	for (var i = 0; i < combatants.length; ++i)
		if(combatants[i].type == "monster")
			total_monsters += 1;
	return total_monsters;
}

function GetTotalPlayers()
{
	var total_m = GetTotalMonsters();
	return combatants.length - total_m;
}

Combatant.prototype.render = function()
{
	var image = LoadImage(this.image);
	image.blit(this.x-20, this.y +50);
	var font = GetSystemFont();
	font.drawText(this.x, this.y, this.name);
	font.drawText(this.x, this.y-20, this.hp);
}

var turn_list = new Array();

function GetAliveMonstersArray()
{
	var total_alive = new Array();
	for (var i = 0; i < combatants.length; ++i)
		if(combatants[i].type == "monster" && combatants[i].hp > 0)
			total_alive.push(i);
	return total_alive;
}

function GetAlivePlayersArray()
{
	var total_alive = new Array();
	for (var i = 0; i < combatants.length; ++i)
		if(combatants[i].type == "player" && combatants[i].hp > 0)
			total_alive.push(i);
	return total_alive;
}

function Attack(from_who, on_who)
{
var str = combatants[from_who].str;
var rand_no = Math.floor((str-(str*0.5))*Math.random()) + (str*0.5);
	combatants[on_who].hp -= rand_no;
}

Combatant.prototype.AI = function()
{
 if(this.type == "player")
 {
		var attack_selection = GetAliveMonstersArray()[0];
		Attack(turn_list[0], attack_selection);
		delay(0.25);
	}
		else if(this.type == "monster")
		{
			var attack_selection = GetAlivePlayersArray()[0];
			Attack(turn_list[0], attack_selection);
			delay(0.25);
		}
}

function RenderBattle()
{
	for (var i = 0; i < combatants.length; ++i)
		combatants[i].render();
}

function Battle()
{
	FlipScreen();
	var run_away = false;
	startBattle();
	while (GetAlivePlayersArray().length > 0 && GetAliveMonstersArray().length > 0&& !run_away)
	{
		if (IsKeyPressed(KEY_R))
			run_away = true;
		if (turn_list.length == 0)
		{
			for (var i = 0; i < combatants.length; ++i)
				turn_list.push(i);
		}
		if (turn_list.length > 0)
		{
			GetKey();
			combatants[turn_list[0]].AI();
			turn_list.shift();
		}
		
		RenderBattle();
		FlipScreen();
	}
	if(GetAlivePlayersArray().length == 0)
	{
		return false;
	}
	else
	{
		return true;
	}
}